define(["jquery","bootstrap","bbcare",
    "scprit/followup/app/prematureOvarianFailure/surveyOfWomenHealth.servers.js",
    "scprit/followup/app/prematureOvarianFailure/surveyOfWomenHealth.event.js",
    "scprit/followup/app/prematureOvarianFailure/surveyOfWomenHealth.ui.js",
    "scprit/followup/app/prematureOvarianFailure/prematureOvarianFailureDetail.ui.js"],function($,bootstrap,$bb,$servers,$event,$serveyUi,$ui){
    var event = {};
    /*
    * 答案渲染：
    * */
    var anwserDeal = function(ques,score){
        var quesId = ques.id;
        if(ques.type == 'single' || ques.type == 'checkbox'){
            if(score && score["ques"][quesId]){
                var value =[];
                for(var j = 0;j<score["ques"][quesId].length;j++){
                    if(ques["option"][score["ques"][quesId][j]]){
                        value.push(score["ques"][quesId][j]+"."+ques["option"][score["ques"][quesId][j]]);
                    }else{
                        value.push(score["ques"][quesId][j]);
                    }
                }
                ques.value = value.join(";");
            }
        }else if(ques.type == "blankList"){
            if(score && score["ques"][quesId]){
                var value =[];
                for(var j = 0;j<score["ques"][quesId].length;j++){
                    value.push({"val":score["ques"][quesId][j],"index":j+1});
                }
                ques.value = value;
            }
        }else if(ques.type == "linkage"){
            for(var l = 0;l<ques.list.length;l++){
                var quesList = ques.list[l];
                anwserDeal(quesList,score);
            }
        }else if(ques.type == "blank"){
            if(score && score["ques"][quesId]){
                ques.value = score["ques"][quesId][0];
            }
        }else if(ques.type == "blankSingle"){
            if(score && score["ques"][quesId]){
                ques.value = score["ques"][quesId][0];
                if(score["ques"][quesId].length > 1){
                    var values = [];
                    for(var i = 0;i<score["ques"][quesId].length;i++){
                        if(i != 0 && i!= (score["ques"][quesId].length-1)){
                            values.push(score["ques"][quesId][i]);
                        }
                    }
                    var option= score["ques"][quesId][score["ques"][quesId].length-1];
                    ques.values = values.join(";");
                    ques.optionVal = option+ "." + ques["option"][option];
                }
            }
        }
    };
    var answerDeal0 = function(questionsnaire,score){
        for(var i = 0;i<questionsnaire.questions.length;i++){
            var ques = questionsnaire.questions[i];
            anwserDeal(ques,score);
        }
    };
    /*
    * 基础问卷处理：
    * */
    var listDeal = function(list,ques){
        var ss= [];
        for(var m = 0;m<list.length;m++) {
            if(list[m].list) {
                var listS = list[m].list;
                for(var i = 0;i < listS.length;i++){
                    if(listS[i].list){
                        for(var j = 0;j < listS[i].list.length;j++){
                            listS[i].list[j].css ="none";
                            ss.push(listS[i].list[j]);
                        }
                    }else{
                        ss.push(listS[i]);
                    }
                }
            }else{
                ss.push(list[m]);
            }
        }
        ques.list = ss;
    };

    var getTotalQuestions_osiList = function(questions){
        for(var i = 0; i< questions.length;i++){
            for(var k = 0;k<questions[i].questions.length;k++){
                if(questions[i].questions[k].list){
                    listDeal(questions[i].questions[k].list, questions[i].questions[k]);
                }
            }
        }
    };
    event.init = function(infoParams){
        var scoreObj = JSON.parse(infoParams.score);
        /*
        * 顶部问卷切换：
        * */
        $("#ques-tabs>div").click(function(){
            var inParams = $.extend({},infoParams);
            $(this).addClass("tab_checked").siblings().removeClass("tab_checked");
            var type = $(this).attr("type");
            inParams.type = type;
            var getQuestionsCallback = function(data){
                var questions = [];
                if(type == "GLE"){
                    var arrItem = ["一般人口学信息","月经婚育史","既往避孕措施情况","近1年内是否出现以下症状","妇科及既往疾病史","家族史","是否诊断为卵巢早衰或卵巢功能减退"];
                    for(var i = 0;i < arrItem.length ; i++){
                        questions.push({"title":arrItem[i],"questions":data.data[arrItem[i]],"group":arrItem[i]});
                    }
                    getTotalQuestions_osiList(questions);
                }else{
                    questions = data.data.quetions;
                }
                /*
                 * 获取每一大项的初始题目序号：//获取长度；
                 * */
                var listQuestions = {};
                for(var i= 0;i<questions.length;i++){
                    questions[i].length = questions[i].questions.length;
                    listQuestions[questions[i].group] = questions[i];
                }
                $ui.questionsItemInit({"id":"#item-list","data":questions});//右侧列表：
                $servers.getTotalQuestions_osi(questions,type);
                var score = scoreObj[type];
                /*
                 * 右侧列表点击事件：
                 * */
                $("#item-list").off("click","li");
                $("#item-list").on("click","li",function(){
                    $(this).addClass("checked").siblings().removeClass("checked");
                    var group = $(this).attr("group");
                    answerDeal0(listQuestions[group],score);
                    $serveyUi.questionsOsiInit({"id":"#wrap_left","data":listQuestions[group]});
                    var questionsnaire = $(".ques-list");
                    questionsnaire.find('li div[type]').each(function(){
                        var id=$(this).attr("id");
                        var type=$(this).attr("type");
                        if(type == "blank"){
                            if(score && score["ques"][id]){
                                $(this).find("span.underline").each(function(index){
                                    $(this).html(score["ques"][id][index]);
                                });
                            }
                        }
                    });
                    /*
                    * 分页按钮：
                    * */
                    var total = listQuestions[group].questions.length;
                    var pages = Math.ceil(total/7);
                    var h = '';
                    for(var i = 1;i <= pages;i++){
                        h += '<button type="button" class="btn btn-default">'+i+'</button>';
                    }
                    $("#btn-group").html(h);
                    /*  分页展示：按钮点击事件  */
                    $("#btn-group").off("click","button");
                    $("#btn-group").on("click","button",function(){
                        $(this).addClass("btn-check").siblings().removeClass("btn-check");
                        var text = $(this).html();
                        var endNo = text*7;
                        $(".panel-body .ques-list li").each(function(){
                            var index = $(this).index();
                            if(endNo - index <= 7 && endNo - index > 0 ){
                                $(this).css("display","block");
                            }else{
                                $(this).css("display","none");
                            }
                        })
                    });
                    $("#btn-group button").eq(0).click();
                });
                $("#item-list li").eq(0).click();
                /*
                 * 点击查看备注：
                 * */
                $("#wrap_left").off("click",".ques-mark");
                $("#wrap_left").on("click",".ques-mark",function(){
                    var mark = $(this).attr("bak");
                    var fuId = $(this).attr("fuId");
                    var title = fuId;
                    if(type == 'GLE')title = title.split("-").join(".");
                    if(mark){
                        mark = JSON.parse(mark);
                        $ui.modalMarkInit({"id":"#myModalBox","mark":mark,"title":title});
                        $("#myModalBox").find("#myModal").modal("show");
                    }else{
                        $bb.toastShow("无备注",1500);
                    }
                })
            };
            $servers.getQuestions(inParams, getQuestionsCallback);
        });
        $("#ques-tabs").find('div.tab').eq(0).click();
    };
    return event;
});